var treeObj = null;
var noPublishTree = null;
var url_tree = "/app/module/listShow";
var url_publish = "/project/" + appId + "/publish";
$(function () {
    treeObj = initTree("#moduleTree", {"appId": appId, "version": appVerson}, false);
    noPublishTree = initTree("#noPublishTree", {"appId": appId, "version": 0}, true);
})

function initTree(treeEl, param, check) {
    var setting = {
        view: {
            dblClickExpand: false,
            showLine: true,
            showIcon: false,
            selectedMulti: false
        },
        check: {
            enable: check
        },
        async: {
            dataType: "json",
            enable: true,
            type: "get",
            otherParam: param,
            url: url_tree,
            dataFilter: function (treeId, parentNode, responseData) {
                return responseData.data;
            }
        },
        data: {
            key: {
                name: "moduleName"
            },
            simpleData: {
                enable: true,
                idKey: "moduleId",
                pIdKey: "pid",
                rootPId: "0"
            }
        },
        callback: {}
    };
    var t = $(treeEl);
    t = $.fn.zTree.init(t, setting);
    return t;
}

function publishVersion() {
    var nodes = noPublishTree.getCheckedNodes();
    if (nodes && nodes.length > 0) {
        var ids = new Array();
        for (var i in nodes) {
            ids.push(nodes[i].moduleId);
        }
        $.ajax({
            type: 'post',
            url: url_publish,
            data: {"ids": ids.join("-")},
            success: function (result) {
                if (result.code != "0000") {
                    showMessage(result.code || result.desc, "error");
                } else {
                    appVerson = result.data / 1;
                    showMessage("发布成功");
                    refreshTree();
                }
            }
        });
    } else {
        showMessage("请勾选要更新的文档", "error");
    }
}

function getParams() {
    return {"appId": appId, "version": appVerson};
}

function refreshTree() {
    treeObj.setting.async.otherParam = getParams();
    treeObj.reAsyncChildNodes(null, "refresh");
    noPublishTree.reAsyncChildNodes(null, "refresh");
}